home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / BlueBox Spy / Blue Box Daemon / source / ScreenGrabber.cp < prev    next >
Encoding:
Text File  |  1999-06-25  |  1.9 KB  |  98 lines  |  [TEXT/CWIE]

  1. #include <ImageCompression.h>
  2. #include <QDOffscreen.h>
  3.  
  4. #include "ScreenGrabber.h"
  5.  
  6.  
  7. #pragma mark Private Functions
  8. #pragma mark -
  9.  
  10. static Boolean    HasQuickTime()
  11. {
  12.     Boolean    has_qt = false;
  13.     SInt32    response;
  14.     
  15.     if ( ( noErr == Gestalt(gestaltQuickTimeVersion, &response) ) &&
  16.          ( response != 0 ) )
  17.     {
  18.         has_qt = true;
  19.     }
  20.     
  21.     return has_qt;
  22. }
  23.  
  24. static OSErr    GetScreenGWorld(GWorldPtr &gworld, SInt16 screen_depth = 16)
  25. {
  26.     OSErr    result = noErr;
  27.     
  28.     result = NewGWorld(&gworld, screen_depth, &qd.screenBits.bounds, nil, nil, useTempMem);
  29.     
  30.     if ( result == noErr )
  31.     {
  32.         CopyBits(&qd.screenBits, &((GrafPtr)gworld)->portBits, &qd.screenBits.bounds, &qd.screenBits.bounds,
  33.                     srcCopy, nil);    
  34.     }
  35.     
  36.     return result;
  37. }
  38.  
  39. #pragma mark -
  40. #pragma mark External Functions
  41. #pragma mark -
  42.  
  43. OSErr    GetScreenJPEG(Handle &h, SInt16 screen_depth)
  44. {
  45.     OSErr                    result = noErr;
  46.     GWorldPtr                gworld = nil;
  47.     SInt32                    max_compress_size;
  48.     ImageDescriptionHandle    image_desc = nil;
  49.     
  50.     h = nil;
  51.     
  52.     if ( ! HasQuickTime() )
  53.         return paramErr;
  54.     
  55.     result = GetScreenGWorld(gworld, screen_depth);
  56.     
  57.     if ( result == noErr )
  58.         result = GetMaxCompressionSize(GetGWorldPixMap(gworld), &qd.screenBits.bounds,
  59.                                                 screen_depth, codecNormalQuality, 'jpeg',
  60.                                                 bestSpeedCodec, &max_compress_size);
  61.                                                 
  62.     if ( result == noErr )
  63.     {
  64.         h = TempNewHandle(max_compress_size, &result);
  65.     
  66.         if ( result == noErr )
  67.             result = MemError();
  68.     }
  69.  
  70.     if ( result == noErr )
  71.     {
  72.         image_desc = (ImageDescriptionHandle)TempNewHandle(0, &result);
  73.     
  74.         if ( result == noErr )
  75.             result = MemError();
  76.     }
  77.     
  78.     if ( result == noErr )
  79.         HLockHi(h);
  80.     
  81.     if ( result == noErr )
  82.         result = CompressImage(GetGWorldPixMap(gworld), &qd.screenBits.bounds, codecNormalQuality,
  83.                                 'jpeg', image_desc, *h);
  84.     
  85.     if ( result == noErr )
  86.     {
  87.         HUnlock(h);
  88.         SetHandleSize(h, (**image_desc).dataSize);    
  89.     }
  90.             
  91.     if ( gworld != nil )
  92.         DisposeGWorld(gworld);
  93.         
  94.     if ( image_desc != nil )
  95.         DisposeHandle((Handle)image_desc);
  96.     
  97.     return noErr;
  98. }